package cn.ytr.crm.service.impl;

import cn.ytr.crm.domain.SystemDictionary;
import cn.ytr.crm.mapper.SystemDictionaryItemMapper;
import cn.ytr.crm.mapper.SystemDictionaryMapper;
import cn.ytr.crm.query.QueryObject;
import cn.ytr.crm.service.ISystemDictionaryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SystemDictionaryServiceImpl implements ISystemDictionaryService{

	@Autowired
	private SystemDictionaryMapper systemDictionaryMapper;
	@Autowired
	private SystemDictionaryItemMapper systemDictionaryItemMapper;

	/**
	 * 增加或保存字典目录
	 */
	@Override
	public void saveOrUpate(SystemDictionary systemDictionary) {
		if (systemDictionary.getId() != null) {
			systemDictionaryMapper.updateByPrimaryKey(systemDictionary);
		} else {
			systemDictionaryMapper.insert(systemDictionary);
		}
	}

	/**
	 * 删除字典目录，注意要维护明细表，把对应的也给删掉
	 */
	@Override
	public void delete(Long id) {
		systemDictionaryMapper.deleteByPrimaryKey(id);
		systemDictionaryItemMapper.deleteByParentId(id);
	}

	/**
	 * 获取单个字典目录
	 */
	@Override
	@Transactional(readOnly=true)
	public SystemDictionary get(Long id) {
		SystemDictionary systemDictionary = systemDictionaryMapper.selectByPrimaryKey(id);
		return systemDictionary;
	}

	/**
	 * 获取所有字典目录
	 */
	@Override
	@Transactional(readOnly=true)
	public List<SystemDictionary> listAll() {
		List<SystemDictionary> list = systemDictionaryMapper.selectAll();
		return list;
	}

	/**
	 * 高级查询所有字典目录
	 */
	@Override
	@Transactional(readOnly=true)
	public PageInfo query(QueryObject qo) {
		/*Integer count = systemDictionaryMapper.selectForCount(qo);
		if (count < 1 || count == null) {
			return new PageResult(qo.getPageSize());
		} else {
			List<SystemDictionary> list = systemDictionaryMapper.selectForList(qo);
			return new PageResult(qo.getCurrentPage(), count, list, qo.getPageSize());
		}*/

		//改用分页插件 pageHelper
		PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
		List<SystemDictionary> list = systemDictionaryMapper.selectForList(qo);
		PageInfo pageInfo = new PageInfo(list);
		return pageInfo;

	}

}
